Skip to content

feat(embeddings): support custom OpenAI baseURL for Ollama / OpenAI-compatible providers#82

Open
pylyp-gh wants to merge 3 commits into
kagent-dev:mainfrom
pylyp-gh:feat/openai-base-url
Open

feat(embeddings): support custom OpenAI baseURL for Ollama / OpenAI-compatible providers#82
pylyp-gh wants to merge 3 commits into
kagent-dev:mainfrom
pylyp-gh:feat/openai-base-url

Conversation

@pylyp-gh
Copy link
Copy Markdown

Summary

Adds optional embedding.openai.base_url config field (and OPENAI_BASE_URL env var fallback) so doc2vec can use any OpenAI-compatible embedding endpoint, not just api.openai.com. Concrete use cases: running doc2vec against a local Ollama instance (nomic-embed-text, mxbai-embed-large models), self-hosted vLLM / LM Studio / llama.cpp servers, or internal LLM gateways with OpenAI-compatible facades.

Why

The OpenAI SDK constructor accepts baseURL for endpoint redirection, but doc2vec currently hardcodes it implicitly to the default (https://api.openai.com/v1). Users who run local embedding stacks have had to fork doc2vec or pay for OpenAI even when they have a perfectly capable local model.

Change

  • types.ts — added base_url?: string to EmbeddingConfig.openai.
  • doc2vec.ts — read embeddingConfig.openai?.base_url || process.env.OPENAI_BASE_URL, spread into the new OpenAI({ ... }) options only when truthy (conditional spread → backwards-compatible).
  • Logger reveals the active endpoint at startup for easier debugging.

Backwards compatibility

Zero impact for existing configs. The field is optional; when absent, the OpenAI SDK falls back to its default base URL — identical to today's behaviour.

Test plan

  • Existing OpenAI flow (no base_url) — unchanged: SDK hits api.openai.com.
  • base_url: http://localhost:11434/v1 + local Ollama — embeddings generated successfully against nomic-embed-text (768d).
  • OPENAI_BASE_URL env var fallback works when config omits the field.

Example config

embedding:
  provider: openai
  dimension: 768
  openai:
    api_key: ollama  # dummy; Ollama does not check
    model: nomic-embed-text
    base_url: http://localhost:11434/v1

…ompatible providers

Adds an optional `base_url` field under `embedding.openai` in the config
schema (and `OPENAI_BASE_URL` env var fallback). When set, it is passed
to the OpenAI SDK constructor as `baseURL`, allowing doc2vec to embed
documents via any OpenAI-compatible endpoint — Ollama, LM Studio, vLLM,
llama.cpp server, internal LLM gateways, etc.

Conditional spread keeps the default behaviour identical when no base
URL is provided; existing configs continue to hit api.openai.com.

The chosen endpoint is now revealed in the startup log line for clearer
diagnostics ("Using OpenAI ... via http://localhost:11434/v1").
…ripts

`npm install --ignore-scripts` (security best-practice — blocks malicious
postinstall scripts) skips node-gyp compilation for native modules. The
better-sqlite3 binding is therefore never built unless a prebuilt binary
exists for the target arch/Node combination.

On linux/arm64 (Apple Silicon, ARM Linux runners) no prebuild ships, so
runtime fails with "Could not locate the bindings file" the moment any
SQLite source tries to open a database.

Adding an explicit `npm rebuild better-sqlite3` after install keeps the
security guarantee for unrelated scripts while compiling the one native
module the tool actually needs.
The reader-side MCP server (mcp/src/index.ts) also instantiated the
OpenAI client with apiKey only. It now reads OPENAI_BASE_URL from the
environment and threads it into the constructor via conditional spread,
mirroring the writer-side fix introduced earlier on this branch.

This lets users point both halves of the doc2vec pipeline (the writer
CLI for building vector DBs and the MCP server for querying them) at
any OpenAI-compatible endpoint — Ollama, LM Studio, vLLM, llama.cpp
server, internal LLM gateways — through a single env var per process.

Default behaviour unchanged when OPENAI_BASE_URL is unset.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants